package com.kyubi.tagger.taggers;

import java.util.Vector;

import com.kyubi.common.config.ConfigKeys;
import com.kyubi.common.config.Configuration;
import com.kyubi.tagger.ITagger;

import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.Word;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;

/**
 * The class representing the decorator which is a wrapper over the
 * Stanford POS tagger.
 * 
 * @author anand
 *
 */
public class POSTagger implements ITagger {

	private ITagger tagger;
	private Configuration config = Configuration.getSingleton();
	private MaxentTagger maxEntTagger;

	/**
	 * 
	 * @param tagger	the previous ITagger object being decorated
	 */
	public POSTagger(ITagger tagger) throws Exception {
		this.tagger = tagger;
		try {
			maxEntTagger = new MaxentTagger(config.get(ConfigKeys.BASEURL)
					+ config.get(ConfigKeys.POS_MODEL_PATH));
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * @see com.kyubi.tagger.ITagger
	 */
	public Vector<String> getTaggedValues() {
		Vector<String> tValues = tagger.getTaggedValues();
		Vector<String> rValues = new Vector<String>();

		try {
			maxEntTagger = new MaxentTagger(config.get(ConfigKeys.BASEURL)
					+ config.get(ConfigKeys.POS_MODEL_PATH));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		for (String value : tValues) {
			String[] tokens = value.split("[ ]+");
			Vector<Word> words = new Vector<Word>();

			// bullshit java - cannot init Collections with String[]
			for (int i = 0; i < tokens.length; i++) {
				words.add(new Word(tokens[i]));
			}

			Sentence sentence = new Sentence(words);
			Sentence tSentence = MaxentTagger.tagSentence(sentence);

			rValues.add(tSentence.toString(false));
		}

		return rValues;
	}
	
	/**
	 * @see com.kyubi.tagger.ITagger
	 */
	public Vector<String> getOriginalValues() {
		Vector<String> dataVect = tagger.getOriginalValues();
		return dataVect;
	}

	public static void main(String[] args) {
		try {
			Configuration c = Configuration.getSingleton(args[2]);
			ITagger tagr = new POSTagger(new XMLWrapper(args[1]));
			tagr.getTaggedValues();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
